package com.example.test.bean;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import com.example.test.bean.GW;

import java.util.ArrayList;
import java.util.List;

public class MySQLiteOpenHelper_1 extends SQLiteOpenHelper {
    private static final String DB_NAME= "mySQLite.db";
    private static final String TABLE_NAME_GW = "GW";
    private static final String CREATE_TABLE_SQL = "create table " +TABLE_NAME_GW + "(id integer primary key autoincrement, carNumber text,date_1 text,w_1 text,date_2 text,w_2 text)";

    public MySQLiteOpenHelper_1(Context context){
        super(context, DB_NAME, null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
//添加车辆
    public long insertData(GW gw){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("carNumber",gw.getCarNumber());
        values.put("date_1",gw.getDate_1());
        values.put("w_1",gw.getW_1());
        return db.insert(TABLE_NAME_GW,null,values);
    }

//    删除车辆
    public int deleteFromDbByCarNumber(String CarNumber){
        SQLiteDatabase db =getWritableDatabase();
        return db.delete(TABLE_NAME_GW,"CarNumber like ?",new String[]{CarNumber});
    }

    //更新回皮的数据
    public int updateData(GW gw){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();//数据分装，用来存数据
        values.put("carNumber",gw.getCarNumber());
        values.put("date_2",gw.getDate_2());
        values.put("w_2",gw.getW_2());
        return db.update(TABLE_NAME_GW,values,"CarNumber like ?",new String[]{gw.getCarNumber()});
    }

    public List<GW> query(String name){
        SQLiteDatabase db=getWritableDatabase();
        List<GW> gwList=new ArrayList<>();
        Cursor cursor=db.query(TABLE_NAME_GW,null,"carNumber like?",new String[]{name+""},null,null,null);
        if (cursor!=null){
            while (cursor.moveToNext()){
                @SuppressLint("Range") String carNumber=cursor.getString(cursor.getColumnIndex("carNumber"));
                @SuppressLint("Range") String date_1=cursor.getString(cursor.getColumnIndex("date_1"));
                @SuppressLint("Range") String date_2=cursor.getString(cursor.getColumnIndex("date_2"));
                @SuppressLint("Range") String w_1=cursor.getString(cursor.getColumnIndex("w_1"));
                @SuppressLint("Range") String w_2=cursor.getString(cursor.getColumnIndex("w_2"));
                GW gw=new GW();
                gw.setCarNumber(carNumber);
                gw.setDate_1(date_1);
                gw.setDate_2(date_2);
                gw.setW_1(w_1);
                gw.setW_2(w_2);
                gwList.add(gw);
            }
            cursor.close();
        }
        return gwList;
    }

}
